package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ProductcategoryDaoImpl;
import com.ysd.entity.Productcategory;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminProductCategoryServlet extends BaseServlet {

	/**
	 * 分页查询所有产品分类 
	 * localhost:8080/meizizi/adminProductCategory?reqName=getProductCates
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */

	public void getProductCates(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ProductcategoryDaoImpl pcDao = new ProductcategoryDaoImpl(); // 创建实现类对象
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		List<Productcategory> pcList = pcDao.getProductCategoryByPage(page, size); // 查到的所有产品分类集合

		// 发送ajax请求,服务器基本都会返回json格式字符串

		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象

		Gson gson = new Gson();
		String res = gson.toJson(pcList); // 将pcList转化为json字符串

		out.print(res); // 响应数据

		out.flush();
		out.close();
	}
	
	
	/**
	 * localhost:8080/meizizi/adminProductCategory?reqName=getAllProductCates
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */

	public void getAllProductCates(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ProductcategoryDaoImpl pcDao = new ProductcategoryDaoImpl(); // 创建实现类对象
		List<Productcategory> pcList = pcDao.getProductcategory(); // 查到的所有产品分类集合

		// 发送ajax请求,服务器基本都会返回json格式字符串

		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象

		Gson gson = new Gson();
		String res = gson.toJson(pcList.size()); // 将"pcList.size()集合的个数"转化为json字符串

		out.print(res); // 响应数据

		out.flush();
		out.close();
	}
	
	
	

	/**
	 * localhost:8080/meizizi/adminProductCategory?reqName=getProductCate
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProductCate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int cId = Integer.parseInt(request.getParameter("categoryId")); // 接收前台传来的参数
		ProductcategoryDaoImpl pcDao = new ProductcategoryDaoImpl(); // 创建实现类对象
		int n = pcDao.deleteProductCateById(cId); // 调用删除的方法

		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象

		if (n > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success":true,"message":"删除成功","remark":""}
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success":false,"message":"删除失败","remark":""}
		}

		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/meizizi/adminProductCategory?reqName=addProCate
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addProCate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象

		ProductcategoryDaoImpl pcDao = new ProductcategoryDaoImpl(); // 创建产品分类的实现类对象

		String productCategoryName = request.getParameter("productCategoryName"); // 接收前台传来的参数
		if (productCategoryName != null && !productCategoryName.equals("")) { // 非空判定
			List<Productcategory> pcList = pcDao.getProductCategoryByName(productCategoryName);
			if (pcList.size() == 0) { // 没有查到可以添加
				Productcategory pc = new Productcategory(0, productCategoryName, null); // 根据接受的参数创建产品分类对象
				int p = pcDao.addProductCategory(pc);
				if (p > 0) {// 添加成功
					out.print(Result.toClient(true, "添加成功", ""));// {"success":true,"message":"添加成功","remark":""}
				} else {// 添加失败
					out.print(Result.toClient(false, "添加失败", ""));// {"success":false,"message":"添加失败","remark":""}
				}
			} else { // 查到了不能添加
				out.print(Result.toClient(false, "分类名称已存在", ""));
			}
		}

		out.flush();// 清空并关闭
		out.close();
	}

	/**
	 * localhost:8080/meizizi/adminProductCategory?reqName=getProductCateById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProductCateById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象

		ProductcategoryDaoImpl pcDao = new ProductcategoryDaoImpl(); // 创建产品分类的实现类对象
		int id = Integer.parseInt(request.getParameter("cId")); // 接收前台传来的参数,并转化为int类型
		Productcategory pc = pcDao.getProductCategoryById(id); // 调用查询方法查到结果

		Gson gson = new Gson();
		String res = gson.toJson(pc); // 将pc转化为json格式字符串

		out.print(res); // 响应数据

		out.flush();// 清空并关闭
		out.close();
	}

	/**
	 * localhost:8080/meizizi/adminProductCategory?reqName=updateProductCateById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateProductCateById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象

		ProductcategoryDaoImpl pcDao = new ProductcategoryDaoImpl(); // 创建产品分类的实现类对象
		int productCategoryId = Integer.parseInt(request.getParameter("proCateId")); // 接收前台传来的参数,并转化为int类型
		String productCategoryName = request.getParameter("proCateName");

		List<Productcategory> pcList = pcDao.getProductCategoryByName(productCategoryName);
		if (pcList.size() == 0) { // 没有查到可以添加
			Productcategory pc = new Productcategory(productCategoryId, productCategoryName);
			int n = pcDao.updateProductCategoryById(pc);
			if (n > 0) {// 修改成功
				out.print(Result.toClient(true, "修改成功", ""));// {"success":true,"message":"修改成功","remark":""}
			} else {// 修改失败
				out.print(Result.toClient(false, "修改失败", ""));// {"success":false,"message":"修改失败","remark":""}
			}
		} else {
			out.print(Result.toClient(false, "分类名称已存在", ""));
		}

		out.flush();// 清空并关闭
		out.close();
	}
	/**
	 * localhost:8080/meizizi/adminProductCategory?reqName=delProductCateByIds
	 * 根据id批量删除产品分类
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delProductCateByIds(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象
		String ids=request.getParameter("ids"); // 接收前台传来的参数
		ProductcategoryDaoImpl pcDao = new ProductcategoryDaoImpl(); // 创建实现类对象
		int n = pcDao.delProductcategoryByIds(ids); // 调用删除的方法
		if (n > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success":true,"message":"删除成功","remark":""}
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success":false,"message":"删除失败","remark":""}
		}

		out.flush();
		out.close();
	}

}
